﻿using System;
using System.Data;
using Model;
using Resources;

public partial class ProblemModify : System.Web.UI.Page
{
    private readonly DirectServiceProblem _DirectServiceProblem = new DirectServiceProblem();
    int problemId;
    private string strProblem = string.Empty; 
    protected void Page_Load(object sender, EventArgs e)
    {
        problemId = GetProblemId();

        pnlBody.Visible = problemId == 0 ? false : true;
        if (problemId == 0) return;

        if (problemId != 0)
        {
            if (ValueUseOtherPage.boolProblem == false)//For avoid diplay data again.
                InitEdit();
        }     
    }
    private void InitEdit()
    {
        DataTable tableEdit = new DataTable();
        tableEdit = _DirectServiceProblem.InitEdit(problemId);
        if (tableEdit.Rows.Count > 0)
        {
            lblTypeequipment.Text = tableEdit.Rows[0]["TYPEEQUIPMENT"].ToString();
            lblGroupproblem.Text = tableEdit.Rows[0]["GROUPEPROBLEM"].ToString();
            txtProblem.Text = tableEdit.Rows[0]["PROBLEM"].ToString();
            ValueUseOtherPage.boolProblem = true;  
        }
    }
    private void UpdateProblem()
    {
        strProblem = txtProblem.Text;
        _DirectServiceProblem.UpdateProblem(problemId, strProblem);
        ValueUseOtherPage.boolProblem = false;
        lblMessageValidate.Text = Resource.MessageValidate; 

    }
    protected void bntValidate_Click(object sender, EventArgs e)
    {
        if (!new Utility().TestPermissions(Resource.PermissionAddProblem))
        {
            lblPermission.Text = Resource.Permission;
            return;
        }
        UpdateProblem();
    }
    public int GetProblemId()
    {
        return !String.IsNullOrEmpty(Request.QueryString["problemId"]) ? Int32.Parse(Request.QueryString["problemId"]) : 0;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        ClearControlDisplay();
    }
    private void ClearControlDisplay()
    {
        txtProblem.Text = "";
    }
}
